#!/bin/bash

# Flutter 跨平台签名信息查看脚本
# 支持 Android, iOS, macOS 签名信息显示

# 设置脚本在出错时继续执行
set +e

# 获取脚本所在目录
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

echo ""
echo "================================================================================"
echo "🔍 Flutter Server Box - 多平台签名信息"
echo "================================================================================"
echo ""

# Android 签名
if [ -d "android" ]; then
    echo "📱 Android 签名信息："
    echo "--------------------------------------------------------------------------------"
    if [ -f "android/key.properties" ]; then
        cd android
        ./gradlew printSigningInfo 2>/dev/null || echo "⚠️  无法读取 Android 签名信息"
        cd ..
    else
        echo "⚠️  Android 签名未配置 (android/key.properties 不存在)"
    fi
    echo ""
fi

# iOS 签名
if [ -d "ios" ]; then
    echo "🍎 iOS 签名证书："
    echo "--------------------------------------------------------------------------------"
    IPHONE_CERTS=$(security find-identity -v -p codesigning 2>/dev/null | grep "iPhone")
    if [ -n "$IPHONE_CERTS" ]; then
        echo "$IPHONE_CERTS"
    else
        echo "⚠️  未找到 iOS 签名证书"
    fi
    echo ""
fi

# macOS 签名
if [ -d "macos" ]; then
    echo "💻 macOS 签名配置："
    echo "--------------------------------------------------------------------------------"
    
    # 检查签名证书
    MAC_CERTS=$(security find-identity -v -p codesigning 2>/dev/null | grep -E "Developer ID|Apple Development|Mac Developer")
    if [ -n "$MAC_CERTS" ]; then
        echo "📋 可用的 macOS 签名证书："
        echo "$MAC_CERTS"
        echo ""
    else
        echo "ℹ️  未找到 macOS 签名证书（开发模式可禁用签名）"
        echo ""
    fi
    
    # 检查当前配置
    if [ -f "macos/Runner.xcodeproj/project.pbxproj" ]; then
        echo "📄 当前 Debug 配置："
        DEBUG_SIGN=$(grep -A 15 "\/\* Debug \*\/ = {" macos/Runner.xcodeproj/project.pbxproj | grep -E "CODE_SIGN_IDENTITY|DEVELOPMENT_TEAM" | head -2)
        if [ -n "$DEBUG_SIGN" ]; then
            echo "$DEBUG_SIGN" | sed 's/^/   /'
        fi
        echo ""
        
        echo "📄 Entitlements 配置："
        if [ -f "macos/Runner/DebugProfile.entitlements" ]; then
            HAS_ICLOUD=$(grep -q "icloud" macos/Runner/DebugProfile.entitlements && echo "✅ 包含 iCloud 权限" || echo "❌ 已移除 iCloud 权限")
            echo "   $HAS_ICLOUD"
        fi
    fi
    
    # 检查 Podfile 配置
    if [ -f "macos/Podfile" ]; then
        HAS_DEPLOYMENT=$(grep -q "MACOSX_DEPLOYMENT_TARGET" macos/Podfile && echo "✅" || echo "❌")
        echo "   $HAS_DEPLOYMENT 部署目标版本修复: $(grep -q "MACOSX_DEPLOYMENT_TARGET.*10.15" macos/Podfile && echo "10.15" || echo "未配置")"
    fi
    echo ""
fi

# 显示配置建议
echo "================================================================================"
echo "📌 配置说明："
echo "================================================================================"
echo ""
echo "✅ macOS 开发模式（已配置）："
echo "   - CODE_SIGN_IDENTITY = \"-\" (禁用签名)"
echo "   - DEVELOPMENT_TEAM = \"\" (无需开发团队)"
echo "   - 移除 iCloud 权限（无需证书）"
echo "   - 部署目标：macOS 10.15+"
echo ""
echo "📱 Android 签名配置："
echo "   1. 创建 android/key.properties"
echo "   2. 配置 build.gradle.kts"
echo "   3. 执行 './查看签名信息.command' 验证"
echo ""
echo "🍎 iOS/macOS 发布签名："
echo "   - 需要 Apple Developer 账号"
echo "   - 使用 Xcode 管理证书和配置文件"
echo "   - Release 配置使用正式签名"
echo ""
echo "================================================================================"
echo "✅ 签名信息显示完成"
echo "================================================================================"
echo ""

